<?php
if (isset($_SESSION['openid'])){
    include '../c/config.php';
    $riqi=date("nj");
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
    $openid=$_SESSION['openid'];
    $cday=date("md");
    $query=$mysqli->query("select id from chou where openid='".$openid."' and riqi='".$riqi."'  limit 0, 1");
    if($query->num_rows>0){
       echo json_encode(array('s'=>'not'));
       exit;
    }
    $mysqli->query("insert into chou(openid,riqi,createtime) values('".$openid."','".$riqi."','".date("Y-m-d H:i:s")."')  ");
    $query=$mysqli->query("select id from chou where openid='".$openid."' order by id desc  limit 0, 1");
    $getInfo=$query->fetch_assoc();
    $getId=$getInfo['id'];
    $query=$mysqli->query("select j from chi where name='".$getId."'  limit 0, 1");
    $jp=5;
    $rand=rand(1,2);
    if (1 == $rand){
    	$jpN=3;
    }else{
    	$jpN=7;
    }
    if($query->num_rows>0){
       $chiInfo=$query->fetch_assoc();
       $jp=$chiInfo['j'];
       if (1 == $jp){
       	 $jpN=2;
       }
       if (2 == $jp){
       	  $jpN=1;
       }
       if (3 == $jp){
       	  if (1 == $rand){$jpN=4;}else{$jpN=6;}  
       }
       if (4 == $jp){
       	if (1 == $rand){$jpN=5;}else{$jpN=8;}
       }
    }
    $query=$mysqli->query("SELECT id FROM `chou` WHERE `jname` != '5' and openid='".$openid."' limit 0, 1");
    if($query->num_rows>0){
    	$jp=5;
        $jpN=3;
    }
    $mysqli->query("update chou set jname='".$jp."' where id='".$getId."' ");   
    echo json_encode(array('s'=>'y', 'l'=> $jp,'n'=>$jpN));
}else{
    echo json_encode(array('s'=>'not2'));
}